package com.xsy.carRental.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xsy.carRental.constant.MessageConstant;
import com.xsy.carRental.dto.VehicleAddDTO;
import com.xsy.carRental.dto.VehiclePageQueryDTO;
import com.xsy.carRental.dto.VehicleUpdateDTO;
import com.xsy.carRental.entity.Vehicle;
import com.xsy.carRental.mapper.VehicleMapper;
import com.xsy.carRental.result.PageResult;
import com.xsy.carRental.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;

    /**
     * 新增车辆
     *
     * @param vehicleAddDTO
     */
    @Override
    public void add(VehicleAddDTO vehicleAddDTO) {

        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddDTO, vehicle);
        vehicle.setStatus(MessageConstant.VEHICLESTATUS_AVAILABLE);
        vehicle.setCreatedAt(LocalDateTime.now());
        log.info("车辆信息 {}", vehicle);
        vehicleMapper.add(vehicle);

    }

    /**
     * 分页查询车辆
     *
     * @param vehiclePageQueryDTO
     * @return
     */
    public PageResult pageQuery(VehiclePageQueryDTO vehiclePageQueryDTO) {

        PageHelper.startPage(vehiclePageQueryDTO.getPage(), vehiclePageQueryDTO.getPageSize());
        Page<Vehicle> page = vehicleMapper.pageQuery(vehiclePageQueryDTO);

        long total = page.getTotal();
        List<Vehicle> vehicles = page.getResult();
        return new PageResult(total, vehicles);
    }


    /**
     * 更新车辆信息
     *
     * @param vehicleUpdateDTO
     */
    @Override
    public void update(VehicleUpdateDTO vehicleUpdateDTO) {
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleUpdateDTO, vehicle);
        vehicle.setCreatedAt(LocalDateTime.now());
        vehicleMapper.update(vehicle);
    }

    /**
     * 根据id删除车辆
     *
     * @param vehicleId
     */
    @Override
    public void delete(int vehicleId) {
        vehicleMapper.delete(vehicleId);
    }

    /**
     * 根据id查询车辆详细信息
     *
     * @param vehicleId
     * @return
     */
    @Override
    public Vehicle getVehicleById(int vehicleId) {
        Vehicle vehicle = vehicleMapper.getVehicleById(vehicleId);
        return vehicle;
    }


}
